package com.scrx.ymg.dao.util;

import java.io.File;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.scrx.ymg.dao.base.BaseDaoImpl;
import com.scrx.ymg.entity.Commission;
import com.scrx.ymg.entity.IndexBanner;

@Repository("utilDao")
@Transactional
public class UtilDaoImpl extends BaseDaoImpl implements IUtilDao {

	public Object getAgentInfoByPassWord(String userName, String passWord) {
		Query query = getCurrentSession().createQuery("from AgentInfo where account=:account and passWord=:passWord");
		query.setParameter("account", userName);
		query.setParameter("passWord", passWord);
		return query.uniqueResult();
	}

	public Object getSuperMarketInfoByPassWord(String userName, String passWord) {
		Query query = getCurrentSession()
				.createQuery("from SuperMarketInfo where account=:account and passWord=:passWord");
		query.setParameter("account", userName);
		query.setParameter("passWord", passWord);
		return query.uniqueResult();
	}

	public Object getYmgHeadByPassWord(String userName, String passWord) {
		Query query = getCurrentSession().createQuery("from YmgHead where account=:account and passWord=:passWord");
		query.setParameter("account", userName);
		query.setParameter("passWord", passWord);
		return query.uniqueResult();
	}

	public Object getOtherInfoByPassWord(String username, String password) {
		Query query = getCurrentSession().createQuery("from OtherInfo where account=:account and passWord=:passWord");
		query.setParameter("account", username);
		query.setParameter("passWord", password);
		return query.uniqueResult();
	}

	public Object getDirectInfoByPassWord(String username, String password) {
		Query query = getCurrentSession().createQuery("from DirectInfo where account=:account and passWord=:passWord");
		query.setParameter("account", username);
		query.setParameter("passWord", password);
		return query.uniqueResult();
	}

	public Commission getCommission() {
		Query query = getCurrentSession().createQuery("from Commission where id=1");
		return (Commission) query.uniqueResult();
	}

	public String updateCommission(Commission comm) {
		try {
			getCurrentSession().update(comm);
			return "true";
		} catch (Exception e) {
			return "false";
		}
	}

	public String createIndexBanner(IndexBanner indexbanner, MultipartFile bannerFile, String licenseImgPath) {
		try {
			getCurrentSession().save(indexbanner);
			bannerFile.transferTo(new File(licenseImgPath));
			return "true";
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			return "false";
		}
	}

	public List<IndexBanner> selectIndexBanner(IndexBanner indexbanner) {
		StringBuffer hql = new StringBuffer("from IndexBanner where 1=1 ");
		init(indexbanner.getId(), "id", hql);
		Query query = getCurrentSession().createQuery(hql.toString());

		return query.list();
	}

	public String updateIndexBanner(IndexBanner indexbanner, MultipartFile bannerFile, String licenseImgPath) {
		try {
			getCurrentSession().update(indexbanner);
			if (bannerFile.getSize() > 0) {
				bannerFile.transferTo(new File(licenseImgPath));
			}
			return "true";
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			e.printStackTrace();
			return "false";
		}
	}

	public List getObjectsByParameter(String class1, String ziduan, Object canshu) {
		try {
			String hql = "from " + class1 + " where " + ziduan + "=:" + ziduan;
			Query query = getCurrentSession().createQuery(hql);
			query.setParameter(ziduan, canshu);
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Object getObjectByParameter(String string, String string2, Object obj) {
		try {
			String hql = "from " + string + " where " + string2 + "=:" + string2;
			Query query = getCurrentSession().createQuery(hql);
			query.setParameter(string2, obj);
			return query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void init(Object obj, Object obj2, StringBuffer hql) {
		if (obj2.equals("userName") && obj != null & obj != "") {
			hql.append(" and " + obj2 + " like '%" + obj + "%'");
		} else if (obj != null && obj != "") {
			hql.append(" and " + obj2 + "='" + obj + "'");
		}
	}

	public String deleteObject(Object objectByParameter) {
		try {
			getCurrentSession().delete(objectByParameter);
			return "true";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
	}

}
